package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPsd;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyBankParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPsdParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyPsdService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.ParseException;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2021-11-03 15:59:35
 */
@Controller
@RequestMapping("/btyPsd")
public class BtyPsdController extends BaseController {

    private String PREFIX = "/modular/system/btyPsd";

    @Autowired
    private BtyPsdService btyPsdService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyPsd.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyPsd_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyPsd_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyPsdParam btyPsdParam) {
        this.btyPsdService.add(btyPsdParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyPsdParam btyPsdParam) {
        this.btyPsdService.update(btyPsdParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyPsdParam btyPsdParam) {
        this.btyPsdService.delete(btyPsdParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyPsdParam btyPsdParam) {
        BtyPsd detail = this.btyPsdService.getById(btyPsdParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-11-03
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyPsdParam btyPsdParam) {
        return this.btyPsdService.findPageBySpec(btyPsdParam);
    }

    /**
     * 配送凭证
     *
     * 4A 其它入库单
     * 4I 其它出库单
     * 4C 销售出库单
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public ResponseData confirmOk(BtyPsdParam param){
        String zdrq=param.getZdrq();
        String billType=param.getCbilltypecode();
        BigDecimal amount=BigDecimal.ZERO;
        //根据单据类型获取总金额
        if(billType.equals("4A")){
         amount=btyPsdService.amountByTypeCode(param);
        }
        if(billType.equals("4I")){
         amount=btyPsdService.vuserdef4ByTypeCode(param);
        }

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
        headers.add("system", "001"); // 系统编码
        headers.add("usercode", "15253172037"); // 用户
        headers.add("password", "514576f58a89767b46c1b44767a08f77");
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        int flag=amount.compareTo(BigDecimal.ZERO);
        StringBuilder sb=new StringBuilder();
        sb.append("{\n" +
                "    \"voucher\":[\n" +
                "        {\n" +
                "            \"details\":[");
        //其它入库单凭证
        if(flag==1&&billType.equals("4A")){
            sb.append("                {\n" +
//                    "                    \"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"73\",\n" +
//                    "                            \"checkvaluecode\":\"030400083\"\n" +//千层饼辅助核算编码
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\"\",\n" +
                    "                    \"debitamount\":\""+amount+"\",\n" +
                    "                    \"explanation\":\"供应链其它类型入库\n\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"1403\",\n" +
                    "                    \"pk_currtype\":\"CNY\"\n" +
                    "                }," +
                    "{\n" +
//                    "\t\t\t\t\"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"96\",\n" +
//                    "                            \"checkvaluecode\":\""+yhk+"\"\n" +
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\""+amount+"\",\n" +
                    "                    \"creditquantity\":\"\",\n" +
                    "                    \"debitamount\":\"\",\n" +
                    "                    \"debitquantity\":\"\",\n" +
                    "                    \"detailindex\":\"\",\n" +
                    "                    \"excrate1\":\"\",\n" +
                    "                    \"explanation\":\"中心其它入库单\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"190101\",\n" +
                    "                    \"pk_currtype\":\"CNY\",\n" +
                    "                    \"price\":\"\"\n" +
                    "                },\n");
        }
        //其它出库单凭证
        if(flag==1&&billType.equals("4I")){
            sb.append("                {\n" +
//                    "                    \"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"73\",\n" +
//                    "                            \"checkvaluecode\":\"030400083\"\n" +//千层饼辅助核算编码
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\"\",\n" +
                    "                    \"debitamount\":\""+amount+"\",\n" +
                    "                    \"explanation\":\"中心其他出库单\n\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"1403\",\n" +
                    "                    \"pk_currtype\":\"CNY\"\n" +
                    "                }," +
                    "{\n" +
//                    "\t\t\t\t\"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"96\",\n" +
//                    "                            \"checkvaluecode\":\""+yhk+"\"\n" +
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\""+amount+"\",\n" +
                    "                    \"creditquantity\":\"\",\n" +
                    "                    \"debitamount\":\"\",\n" +
                    "                    \"debitquantity\":\"\",\n" +
                    "                    \"detailindex\":\"\",\n" +
                    "                    \"excrate1\":\"\",\n" +
                    "                    \"explanation\":\"中心其他出库单\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"190101\",\n" +
                    "                    \"pk_currtype\":\"CNY\",\n" +
                    "                    \"price\":\"\"\n" +
                    "                },\n");
        }
        //销售出库凭证
        if(flag==1&&billType.equals("4C")){
            sb.append("                {\n" +
//                    "                    \"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"73\",\n" +
//                    "                            \"checkvaluecode\":\"030400083\"\n" +//千层饼辅助核算编码
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\"\",\n" +
                    "                    \"debitamount\":\""+amount+"\",\n" +
                    "                    \"explanation\":\"销售出库\n\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"1403\",\n" +
                    "                    \"pk_currtype\":\"CNY\"\n" +
                    "                }," +
                    "{\n" +
//                    "\t\t\t\t\"ass\":[\n" +
//                    "                        {\n" +
//                    "                            \"checktypecode\":\"2\",\n" +
//                    "                            \"checkvaluecode\":'"+yaoma+"'\n" +
//                    "                        },\n" +
//                    "\t\t\t\t\t\t{\n" +
//                    "                            \"checktypecode\":\"96\",\n" +
//                    "                            \"checkvaluecode\":\""+yhk+"\"\n" +
//                    "                        }\n" +
//                    "                    ],\n" +
                    "                    \"creditamount\":\""+amount+"\",\n" +
                    "                    \"creditquantity\":\"\",\n" +
                    "                    \"debitamount\":\"\",\n" +
                    "                    \"debitquantity\":\"\",\n" +
                    "                    \"detailindex\":\"\",\n" +
                    "                    \"excrate1\":\"\",\n" +
                    "                    \"explanation\":\"销售出库\",\n" +
                    "                    \"localcreditamount\":\"\",\n" +
                    "                    \"localdebitamount\":\"\",\n" +
                    "                    \"pk_accsubj\":\"6001\",\n" +
                    "                    \"pk_currtype\":\"CNY\",\n" +
                    "                    \"price\":\"\"\n" +
                    "                },\n");
        }
        sb.append("            ],\n" +
                "            \"pk_corp\":\"201\",\n" +
                "            \"pk_glorgbook\":\"201-bty01\",\n" +
                "            \"pk_prepared\":\"15253172037\",\n" +
                "            \"pk_vouchertype\":\"记账\",\n" +
                "            \"prepareddate\":"+zdrq+"\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        System.out.println("sb:"+sb.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
        String url = "http://124.70.28.147:8088/u8cloud/api/gl/voucher/insert";
        String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
        System.out.println("result:"+result);
        JsonParser jp = new JsonParser();
        //将json字符串转化成json对象
        JsonObject jo = jp.parse(result).getAsJsonObject();
        //获取errcode对应的值
        String status = jo.get("status").getAsString();

        if(status.equals("falied")){
            String errormsg = jo.get("errormsg").getAsString();
            return  ResponseData.error(501,errormsg);
        }else{
            return ResponseData.success();
        }
    }

}


